{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c3cf5267",
   "metadata": {},
   "source": [
    "# API开放文档\n",
    "## 本周主要内容：图像识别（认知服务）\n",
    "* 22"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb625a0e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-29T11:53:44.390480Z",
     "start_time": "2022-03-29T11:53:44.376087Z"
    }
   },
   "source": [
    "# 百度AI开放平台\n",
    "## 通用物体和场景识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f880c1a5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd4b3f49",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b792759",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "9ffeb008",
   "metadata": {},
   "source": [
    "## 鉴权认证机制\n",
    "* 获取Access Token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bf165694",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-29T12:33:25.458133Z",
     "start_time": "2022-03-29T12:33:25.281465Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.0c2a0216eb2c9298185647874854e840.315360000.1963917205.282335-25869192', 'expires_in': 2592000, 'session_key': '9mzdD0bbKgeP4sdL9Pzc3SiCoxR6niAhN7gQABCwQSaPJSM8cKiFY2iBsC4Wt0PJ2Tn53R6BmQjiFwXOpUwoVjWU4ICAmQ==', 'access_token': '24.4bee7a66a10b08afde4a680b96080888.2592000.1651149205.282335-25869192', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': 'e7f0103a0997f63292286fd4d8b629a6'}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "# 准备url参数（酬载）\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'UsMyXIv6d4FLymxFL6Cb6CMK',\n",
    "    'client_secret':'iGf41KwirB1osBIuzpP05Lkx6M9WoYE5'\n",
    "}\n",
    "response = requests.get(host,params = payload)\n",
    "if response:\n",
    "    print(response.json())\n",
    "    access_token = response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "684fb96b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-29T12:33:25.970497Z",
     "start_time": "2022-03-29T12:33:25.952027Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0c9189f",
   "metadata": {},
   "source": [
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bbbb4a2c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-29T12:33:27.456600Z",
     "start_time": "2022-03-29T12:33:27.090978Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b70acda7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-29T12:33:32.857477Z",
     "start_time": "2022-03-29T12:33:27.640749Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[             参数                                                  值\n",
       " 0  access_token  通过API Key和Secret Key获取的access_token,参考“Access ...,\n",
       "              参数                                  值\n",
       " 0  Content-Type  application/x-www-form-urlencoded,\n",
       "           参数       是否必选       类型 可选值范围  \\\n",
       " 0      image    和url二选一   string     -   \n",
       " 1        url  和image二选一   string     -   \n",
       " 2  baike_num          否  integer     -   \n",
       " \n",
       "                                                   说明  \n",
       " 0  图像数据，base64编码，要求base64编码后大小不超过4M，最短边至少15px，最长边...  \n",
       " 1  图片完整URL，URL长度不超过1024字节，URL对应的图片base64编码后大小不超过4...  \n",
       " 2                                   返回百科信息的结果数，默认不返回  ,\n",
       "               字段 是否必选             类型                               说明\n",
       " 0         log_id    是         uint64                 唯一的log id，用于问题定位\n",
       " 1     result_num    是         unit32  返回结果数目，及result数组中的元素个数，最多返回5个结果\n",
       " 2         result    是  arrry(object)                           标签结果数组\n",
       " 3       +keyword    是         string                      图片中的物体或场景名称\n",
       " 4         +score    是          float                          置信度，0-1\n",
       " 5          +root    是         string   识别结果的上层标签，有部分钱币、动漫、烟酒等tag无上层标签\n",
       " 6    +baike_info    否         object                    对应识别结果的百科词条名称\n",
       " 7    ++baike_url    否         string                   对应识别结果百度百科页面链接\n",
       " 8    ++image_url    否         string                     对应识别结果百科图片链接\n",
       " 9  ++description    否         string                     对应识别结果百科内容描述]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html('https://ai.baidu.com/ai-doc/IMAGERECOGNITION/Xk3bcxe21')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ed3bc05b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-29T12:53:46.615283Z",
     "start_time": "2022-03-29T12:53:46.604311Z"
    }
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character in identifier (<ipython-input-8-17e4da3e58cb>, line 22)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-8-17e4da3e58cb>\"\u001b[1;36m, line \u001b[1;32m22\u001b[0m\n\u001b[1;33m    'image':img，\u001b[0m\n\u001b[1;37m               ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character in identifier\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "# f = open('C:/Users/86137/Desktop/API-2022-02/API/week05/xihu.jpg', 'rb')\n",
    "f = open('xihu.jpg','rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# params = {\"image\":img}\n",
    "# access_token = '[调用鉴权接口获取的token]'\n",
    "# request_url = request_url + \"?access_token=\" + access_token\n",
    "# 写url的参数（酬载）\n",
    "payload = {\n",
    "    'access_token':access_token,\n",
    "    'image':img，\n",
    "    'baike_num':5\n",
    "    \n",
    "}\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, params = payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4e251ce",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
